/*
 * @Description: 
 * @Author: charles
 * @Date: 2021-11-15 14:30:09
 * @LastEditors: charles
 * @LastEditTime: 2021-11-15 16:22:42
 */
import {createStore,combineReducers,applyMiddleware} from 'redux'
import createSagaMiddleware from 'redux-saga'
import saga from './saga'
// 1. reducer
function todos(state={ list:[], loading:false },action){
  switch (action.type) {
    case "ADD_TODO":
      return {
        ...state,
        list:state.list.concat(action.payload)  // 深拷贝
      }
    default:
      return state;
  }
}
function articles(state={data:{list:[]},loading:false},action){
  switch (action.type) {
    case "REFRESH_ARTICLES":
      return {
        ...state,
        data:action.payload
      }
    default:
      return state;
  }
}
// 2. 创建store
// 2.1 创建saga中间件
const sagaMiddleware = createSagaMiddleware();
const store = createStore(combineReducers({
  todos,
  articles
}),applyMiddleware(sagaMiddleware));  // 2.2将saga集成到状态机
// 2.3 运行中间件
sagaMiddleware.run(saga)
// 3. 返回store
export default store;
